home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ For TASM / USRGUIDE.PAK / VAREXCH.ASM < prev    next >
Assembly Source File  |  1996-02-21  |  2KB  |  46 lines

  1. ; Turbo Assembler example. Copyright (c) 1993 By Borland International, Inc.
  2. ;
  3. ; VAREXCH.ASM
  4. ;
  5. ; Usage: Run tasm on this file and link with varexch.pas
  6.  
  7. CODE      SEGMENT
  8.           ASSUME cs:CODE,ds:NOTHING
  9.  
  10. ; Parameters (note that offset are +2 because of push bp)
  11.  
  12. var1      EQU    DWORD PTR ss:[bp+12]
  13. var2      EQU    DWORD PTR ss:[bp+8]
  14. count     EQU    WORD PTR  ss:[bp+6]
  15.  
  16.  
  17. Exchange  PROC FAR
  18.           PUBLIC Exchange
  19.           cld                           ;exchange goes upward
  20.           mov     dx,ds                 ;save DS
  21.           push    bp
  22.           mov     bp,sp                 ;get stack base
  23.           lds     si,var1               ;get first address
  24.           les     di,var2               ;get second address
  25.           mov     cx,count              ;get number of bytes to move
  26.           shr     cx,1                  ;get word count (low bit -> carry)
  27.           jnc     ExchangeWords         ;if no odd byte, enter loop
  28.           mov     al,es:[di]            ;read odd byte from var2
  29.           movsb                         ;move a byte from var1 to var2
  30.           mov     [si-1],al             ;write var2 byte to var1
  31.           jz      Finis                 ;done if only 1 byte to exchange
  32. ExchangeWords:
  33.           mov     bx,-2                 ;BX is a handy place to keep -2
  34. ExchangeLoop:
  35.           mov     ax,es:[di]            ;read a word from var2
  36.           movsw                         ;do a move from var1 to var2
  37.           mov     [bx][si],ax           ;write var2 word to var1
  38.           loop    ExchangeLoop          ;repeat "count div 2" times
  39. Finis:
  40.           mov     ds,dx                 ;get back Turbo's DS
  41.           pop     bp
  42.           ret     10
  43. Exchange  ENDP
  44. CODE      ENDS
  45.           END
  46.